/**
 * 
 */
package fr.mynounou.web.model;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import fr.mynounou.entities.Nounou;
import fr.mynounou.entities.contrats.Contrat;
import fr.mynounou.exceptions.dao.ExistingEntityException;
import fr.mynounou.web.model.dao.ContratDao;

/**
 * @author btavernier
 *
 */
@SuppressWarnings("serial")
@ManagedBean(name = "ContratsManager")
@ApplicationScoped
public class ContratsManager implements Serializable {
	@ManagedProperty(value="ContratDAO")
	ContratDao dao;
	
	public List<Contrat> listContrats(Nounou n){
		return dao.listContrats(n);
	}
	
	public void persistContrat(Contrat c) throws ExistingEntityException{
		for(Contrat con:dao.listContrats(c.getNounou())){
			if(con.getEnfant().getNom().equals(c.getEnfant().getNom())){
				if(con.getEnfant().getPrenom().equals(c.getEnfant().getPrenom())){
					throw new ExistingEntityException("Contrat existant");
				}
			}
		}
		dao.createContrat(c);
	}
}
